package com.yuanluludev.gaode.utils

import androidx.fragment.app.FragmentActivity
import com.amap.api.location.AMapLocationClient
import com.yuanluludev.gaode.GaodeLoc
import com.yuanluludev.gaode.utils.LocPermissionUtils.getLocPermission
import com.yuanluludev.gaode.utils.LocPermissionUtils.isLocPermissionGranted
import com.yuanluludev.lib_utils.util.Utils
import com.yuanluludev.permission.RxPermissionUtils

/**
 * yuanLuLu
 * 申请定位权限
 * 针对 Android 10 之前和之后的定位权限判断
 * 申请定位权限然后定位
 * 判断定位权限然后定位
 */
object LocUtils {

    /**
     * 标记是否是后台定位
     */
    var isBackgroundLoc: Boolean = false

    /**
     * 定位循环间隔
     */
    var locPeriod: Long = 3000

    /**
     * 进行合规检查
     */
    fun init(isBackgroundLoc: Boolean, locPeriod: Long = 3000) {
        this.isBackgroundLoc = isBackgroundLoc
        this.locPeriod = locPeriod
        AMapLocationClient.updatePrivacyShow(Utils.getApp(), true, true)
        AMapLocationClient.updatePrivacyAgree(Utils.getApp(), true)
    }

    /**
     * 申请定位权限
     */
    fun requestLocPermission(
        activity: FragmentActivity,
        onGranted: (() -> Unit)? = null,
        isShowGuideDialog: Boolean = true
    ) {
        //前台定位
        if (!isBackgroundLoc) {
            RxPermissionUtils.requestPermissionInActivity(
                activity = activity,
                permissions = getLocPermission(),
                onGranted = onGranted,
                isShowGuideDialog = isShowGuideDialog
            )
        } else { //后台定位 必须要分开申请权限 否则直接没有弹窗 直接授权失败
            RxPermissionUtils.requestPermissionInActivity(
                activity = activity,
                permissions = getLocPermission(times = LocPermissionUtils.Times.FIRST),
                onGranted = {
                    RxPermissionUtils.requestPermissionInActivity(
                        activity = activity,
                        permissions = getLocPermission(times = LocPermissionUtils.Times.SECOND),
                        onGranted = onGranted,
                        isShowGuideDialog = isShowGuideDialog
                    )
                },
                isShowGuideDialog = isShowGuideDialog
            )
        }
    }

    /**
     * 申请定位权限并且定位
     */
    fun requestLocPermissionAndLoc(
        activity: FragmentActivity,
        isShowGuideDialog: Boolean = true
    ) {
        //前台定位
        if (!isBackgroundLoc) {
            RxPermissionUtils.requestPermissionInActivity(
                activity = activity,
                permissions = getLocPermission(),
                onGranted = { GaodeLoc.startLoc() },
                isShowGuideDialog = isShowGuideDialog
            )
        } else { //后台定位 必须要分开申请权限 否则直接没有弹窗 直接授权失败
            RxPermissionUtils.requestPermissionInActivity(
                activity = activity,
                permissions = getLocPermission(times = LocPermissionUtils.Times.FIRST),
                onGranted = {
                    RxPermissionUtils.requestPermissionInActivity(
                        activity = activity,
                        permissions = getLocPermission(times = LocPermissionUtils.Times.SECOND),
                        onGranted = { GaodeLoc.startLoc() },
                        isShowGuideDialog = isShowGuideDialog
                    )
                },
                isShowGuideDialog = isShowGuideDialog
            )
        }
    }

    /**
     * 判断权限并且定位（不会申请定位权限 权限通过之后才会定位！！！）
     */
    fun judgeLocPermissionAndLoc() {
        if (isLocPermissionGranted()) {
            GaodeLoc.startLoc()
        }
    }

}